package mypack;

import java.util.Arrays;

public class Chromosome {
	public int mGenMap[];
	public int parent1[];
	public int parent2[];
	public int mPathLength;
	public float mFitness;
	int crossPoint1;
	int crossPoint2;
	int numberOfChosen=0;
	public int mRankFitness = 0;
	
	
	public Chromosome(int pGenMap[])
	{
		mGenMap = pGenMap;
	}

	public Chromosome()
	{
		mGenMap = new int[Global.CHROMOSOME_LENGTH];
		parent1 = new int[Global.CHROMOSOME_LENGTH];
		parent2 = new int[Global.CHROMOSOME_LENGTH];
		for (int i = 0; i < Global.CHROMOSOME_LENGTH; i++) 
		{
			mGenMap[i] = -1;
			parent1[i] = -1;
			parent2[i] = -1;
			
		}
		mPathLength = 0;
		mFitness = 0;
	}
	
	public void calculatePathLength()
	{
		mPathLength = 0;
		int index1 = -1;
		int index2 = -2;
		int dist = -1;
		for (int i = 0; i < Global.CHROMOSOME_LENGTH-1; i++) 
		{
			index1 = mGenMap[i];
			index2 = mGenMap[i+1];
			dist = Global.gen.AdjencyMatrix[index1][index2];
			mPathLength += dist;
		}
		index1 = mGenMap[Global.CHROMOSOME_LENGTH-1];
		index2 = mGenMap[0];
		dist = Global.gen.AdjencyMatrix[index1][index2];
		mPathLength += dist;
	}
	
	public boolean isCityInTheTour(int cityID)
	{
		for (int i = 0; i < Global.CHROMOSOME_LENGTH; i++) 
		{
			if (mGenMap[i] == cityID) 
			{
				return true;
			}
		}
		return false;
	}
	
	public Chromosome copyChromosome()
	{
		Chromosome newOne = new Chromosome();
		newOne.crossPoint1 = crossPoint1;
		newOne.crossPoint2 = crossPoint2;
		newOne.mFitness = mFitness;
		newOne.mGenMap = Arrays.copyOf(mGenMap, Global.CHROMOSOME_LENGTH);
		newOne.mPathLength = mPathLength;
		newOne.parent1 = Arrays.copyOf(parent1, Global.CHROMOSOME_LENGTH);
		newOne.parent2 = Arrays.copyOf(parent2, Global.CHROMOSOME_LENGTH);
		newOne.mRankFitness = mRankFitness;
		return newOne;
	}
	
	public boolean isSame(Chromosome pChromosome)
	{
		for (int i = 0; i < mGenMap.length; i++) 
		{
			if (this.mGenMap[i] != pChromosome.mGenMap[i]) 
			{
				return false;
			}
		}
		return true;
	}
	
	public int similarityPercentage(Chromosome pChoromosome)
	{
		int sameGenNumber=0;
		for(int i = 0; i < Global.CHROMOSOME_LENGTH; i++)
		{
			if(mGenMap[i] == pChoromosome.mGenMap[i])
			{
				sameGenNumber++;
			}
		}
		
		return (100 * sameGenNumber / Global.CHROMOSOME_LENGTH);
	}
	
	public int findIndex(int pVal)
	{
		//write search algorithm
		return -1;
	}
	
	public int findNextIndex (int pVal)
	{
		return (findIndex(pVal)+1)%Global.CHROMOSOME_LENGTH;
	}
}
